package myky.com.doctorapp.adapter;

/**
 * Created by xiangyao on 2018/12/8.
 */

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Title:医生资讯viewpager的adapter
 * Description:人为干预Fragment的生命周期
 *
 * @author xiangyao
 * date 2018/12/10
 */

public class DoctorInfoViewPagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
    private List<Fragment> fragments; // 每个Fragment对应一个Page
    private FragmentManager fragmentManager;
    private ViewPager viewPager; // viewPager对象
    private int currentPageIndex = 0; // 当前page索引（切换之前）


    public DoctorInfoViewPagerAdapter(FragmentManager fragmentManager, ViewPager viewPager, List<Fragment> fragments) {
        this.fragments = fragments;
        this.fragmentManager = fragmentManager;
        this.viewPager = viewPager;
        this.viewPager.setAdapter(this);
        this.viewPager.setOnPageChangeListener(this);






    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(fragments.get(position).getView()); // 移出viewpager两边之外的page布局
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = fragments.get(position);
        if (!fragment.isAdded()) { // 如果fragment还没有added
            FragmentTransaction ft = fragmentManager.beginTransaction();
            ft.add(fragment, fragment.getClass().getSimpleName());
            ft.commit();
            /**
             * 在用FragmentTransaction.commit()方法提交FragmentTransaction对象后
             * 会在进程的主线程中，用异步的方式来执行。
             * 如果想要立即执行这个等待中的操作，就要调用这个方法（只能在主线程中调用）。
             * 要注意的是，所有的回调和相关的行为都会在这个调用中被执行完成，因此要仔细确认这个方法的调用位置。
             */
            fragmentManager.executePendingTransactions();
        }

        if (fragment.getView().getParent() == null) {
            container.addView(fragment.getView()); // 为viewpager增加布局
        }

        return fragment.getView();
    }

    private float lastValue;
    private boolean isScrolling = false;  //viewpager是否滑动
    private boolean isRightToLeft = false;  //从右向左滑动
    private boolean isLeftToRight = false;  //从左向右滑动

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (positionOffset == 0.0) {
            return;
        }
        if (lastValue > positionOffset) {
            // 递减，从左向右滑动  前
            isRightToLeft = false;
            isLeftToRight = true;
        } else if (lastValue < positionOffset) {
            // 递增，从右向左滑动 后
            isRightToLeft = true;
            isLeftToRight = false;
        } else if (lastValue == positionOffset) {
            isLeftToRight = isRightToLeft = false;
        }
        lastValue = positionOffset;
        isScrolling = true;


    }

    @Override
    public void onPageSelected(int i) {
        fragments.get(currentPageIndex).onHiddenChanged(true);
        if (fragments.get(i).isAdded()) {
            fragments.get(i).onHiddenChanged(false);

        }
        currentPageIndex = i;

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


}